	package net.thermetics.fx.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.thermetics.fx.auth.GaeUser;
import net.thermetics.fx.services.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * This controller routes requests to the appropriate user resource for
 * the requested URI and HTTP request method.
 * @author projectmoon
 *
 */
@Controller
@RequestMapping("api")
public class UserController extends AbstractController {
	
	private UserService service;

	@Autowired
	@Qualifier("userService")
	public void setUserService(UserService userService) {
		this.service = userService;
	}

	@RequestMapping(value = "/users", method = RequestMethod.GET)
	public @ResponseBody List<String> getUserList() {				
		List<GaeUser> users = service.getAllUsers();
		List<String> userLinks = new ArrayList<String>(users.size());
		
		for (GaeUser user : users) {
			String link = "/app/api/users/" + user.getUserId();
			userLinks.add(link);
		}
		
		return userLinks;
	}
	
	@RequestMapping(value = "/users/{username}", method = RequestMethod.GET)
	public @ResponseBody Object getUser(@PathVariable String username) {
		GaeUser user = service.getUser(username);
		
		if (user != null) {
			return user;
		}
		else {
			return Boolean.FALSE;
		}
	}
		
	@Secured("ROLE_USER")
	@RequestMapping(value = "/users/{username}", method = RequestMethod.PUT)
	public @ResponseBody boolean updateUser(@PathVariable String username, @RequestBody GaeUser user) {
		if (currentUser != null && currentUser.getPrincipal().getUserId().equals(username)) {
			return service.updateUser(user);
		}
		else {
			return false;
		}
	}

	@Secured("ROLE_USER")
	@RequestMapping(value = "/users/{username}", method = RequestMethod.DELETE)
	public @ResponseBody boolean updateUser(@PathVariable String username) {		
		if (currentUser != null && currentUser.getPrincipal().getUserId().equals(username)) {
			return service.deleteUser(username);
		}
		else {
			return false;
		}
	}
	
	@RequestMapping(value = "/users/current/prefs/{key}", method = RequestMethod.GET)
	public @ResponseBody Object getUserPref(@PathVariable String key) {
		//Tiny class to encapsulate result.
		class Pref {
			@SuppressWarnings("unused")	public String key;
			@SuppressWarnings("unused") public String value;
		}
		
		if (currentUser != null && currentUser.getPrincipal().getPrefs() != null) {
			Map<String, String> prefs = currentUser.getPrincipal().getPrefs();
			String value = prefs.get(key);
			Pref pref = new Pref();
			pref.key = key;	
			pref.value = value;
			return pref;
		}
		else {
			return "null"; //TODO: Uh, we should like, yeahhh...
		}
	}
	
	@RequestMapping(value = "/users/current/prefs/{key}", method = RequestMethod.PUT)
	public @ResponseBody boolean setUserPref(@PathVariable String key, @RequestBody String value) {
		if (currentUser != null && currentUser.getPrincipal().getPrefs() != null) {
			Map<String, String> prefs = currentUser.getPrincipal().getPrefs();
			prefs.put(key, value);
			return service.updateUser(currentUser.getPrincipal());
		}
		else {
			return false;
		}
	}	
}
